/*   1:    */ package com.sie.driver.ftusbdriver.ft4222;
/*   2:    */ 
/*   3:    */ import android.util.Log;

import com.sie.driver.ftusbdriver.FT_Device;
import com.sie.driver.ftusbdriver.interfaces.Gpio;
/*   6:    */ 
/*   7:    */ public class FT_4222_Gpio
/*   8:    */   implements Gpio
/*   9:    */ {
/*  10:    */   private FT_4222_Device mFT4222Device;
/*  11:    */   private FT_Device mFtDev;
/*  12:    */   static final int TOTAL_GPIOS = 4;
/*  13: 49 */   boolean iSignal = true;
/*  14:    */   static final int GET_STATUS = 32;
/*  15:    */   static final int SET_DIRECTION = 33;
/*  16:    */   static final int GET_DIRECTION = 33;
/*  17:    */   static final int SET_PULL_UP = 34;
/*  18:    */   static final int GET_PULL_UP = 34;
/*  19:    */   static final int SET_OPEN_DRAIN = 35;
/*  20:    */   static final int GET_OPEN_DRAIN = 35;
/*  21:    */   static final int SET_PULL_DOWN = 36;
/*  22:    */   static final int GET_PULL_DOWN = 36;
/*  23:    */   
/*  24:    */   public FT_4222_Gpio(FT_4222_Device ft4222Device)
/*  25:    */   {
/*  26: 64 */     this.mFT4222Device = ft4222Device;
/*  27: 65 */     this.mFtDev = this.mFT4222Device.mFtDev;
/*  28:    */   }
/*  29:    */   
/*  30:    */   public int cmdSet(int wValue1, int wValue2)
/*  31:    */   {
/*  32: 69 */     return this.mFtDev.VendorCmdSet(33, wValue1 | wValue2 << 8);
/*  33:    */   }
/*  34:    */   
/*  35:    */   public int cmdSet(int wValue1, int wValue2, byte[] buf, int datalen)
/*  36:    */   {
/*  37: 73 */     return this.mFtDev.VendorCmdSet(33, wValue1 | wValue2 << 8, buf, datalen);
/*  38:    */   }
/*  39:    */   
/*  40:    */   public int cmdGet(int wValue1, int wValue2, byte[] buf, int datalen)
/*  41:    */   {
/*  42: 77 */     return this.mFtDev.VendorCmdGet(32, wValue1 | wValue2 << 8, buf, datalen);
/*  43:    */   }
/*  44:    */   
/*  45:    */   public int init(int[] gpio)
/*  46:    */   {
/*  47: 90 */     chiptop_mgr chipStatus = this.mFT4222Device.mChipStatus;
/*  48: 91 */     gpio_dev gpioStatus = new gpio_dev();
/*  49:    */     
/*  50: 93 */     byte[] data = new byte[1];
/*  51: 94 */     gpio_mgr gpioMgr = new gpio_mgr();
/*  52:    */     
/*  53: 96 */     cmdSet(7, 0);
/*  54: 97 */     cmdSet(6, 0);
/*  55:    */     
/*  56:    */ 
/*  57:    */ 
/*  58:101 */     int ftStatus = this.mFT4222Device.init();
/*  59:102 */     if (ftStatus != 0)
/*  60:    */     {
/*  61:103 */       Log.e("GPIO_M", "FT4222_GPIO init - 1 NG ftStatus:" + ftStatus);
/*  62:104 */       return ftStatus;
/*  63:    */     }
/*  64:107 */     if ((chipStatus.chip_mode == 2) || (chipStatus.chip_mode == 3)) {
/*  65:109 */       return 1013;
/*  66:    */     }
/*  67:112 */     getStatus(gpioStatus);
/*  68:113 */     byte dir = gpioStatus.dir;
/*  69:114 */     data[0] = gpioStatus.dat[0];
/*  70:116 */     for (int idx = 0; idx < 4; idx++) {
/*  71:118 */       if (gpio[idx] == 1) {
/*  72:120 */         dir = (byte)((dir | 1 << idx) & 0xF);
/*  73:    */       } else {
/*  74:124 */         dir = (byte)(dir & (1 << idx ^ 0xFFFFFFFF) & 0xF);
/*  75:    */       }
/*  76:    */     }
/*  77:128 */     gpioMgr.lastGpioData = data[0];
/*  78:129 */     cmdSet(33, dir);
/*  79:    */     
/*  80:131 */     return 0;
/*  81:    */   }
/*  82:    */   
/*  83:    */   public int read(int portNum, boolean[] bValue)
/*  84:    */   {
/*  85:146 */     gpio_dev gpioStatus = new gpio_dev();
/*  86:    */     
/*  87:148 */     int ftStatus = check(portNum);
/*  88:149 */     if (ftStatus != 0) {
/*  89:150 */       return ftStatus;
/*  90:    */     }
/*  91:152 */     ftStatus = getStatus(gpioStatus);
/*  92:153 */     if (ftStatus != 0) {
/*  93:154 */       return ftStatus;
/*  94:    */     }
/*  95:156 */     getGpioPinLevel(portNum, gpioStatus.dat[0], bValue);
/*  96:    */     
/*  97:158 */     return 0;
/*  98:    */   }
/*  99:    */   
/* 100:    */   public int newRead(int portNum, boolean[] bValue)
/* 101:    */   {
/* 102:166 */     int ftStatus = check(portNum);
/* 103:167 */     if (ftStatus != 0) {
/* 104:168 */       return ftStatus;
/* 105:    */     }
/* 106:170 */     int iSize = this.mFtDev.getQueueStatus();
/* 107:172 */     if (iSize > 0)
/* 108:    */     {
/* 109:173 */       byte[] bData = new byte[iSize];
/* 110:174 */       this.mFtDev.read(bData, iSize);
/* 111:175 */       getGpioPinLevel(portNum, bData[(iSize - 1)], bValue);
/* 112:    */     }
/* 113:    */     else
/* 114:    */     {
/* 115:177 */       return -1;
/* 116:    */     }
/* 117:    */     byte[] bData;
/* 118:179 */     return iSize;
/* 119:    */   }
/* 120:    */   
/* 121:    */   public int write(int portNum, boolean bValue)
/* 122:    */   {
/* 123:194 */     gpio_dev gpioStatus = new gpio_dev();
/* 124:    */     
/* 125:196 */     int ftStatus = check(portNum);
/* 126:197 */     if (ftStatus != 0) {
/* 127:198 */       return ftStatus;
/* 128:    */     }
/* 129:200 */     if (!is_GPIOPort_Valid_Output(portNum)) {
/* 130:202 */       return 1015;
/* 131:    */     }
/* 132:205 */     getStatus(gpioStatus);
/* 133:207 */     if (bValue)
/* 134:    */     {
/* 135:208 */       int tmp50_49 = 0; byte[] tmp50_46 = gpioStatus.dat;tmp50_46[tmp50_49] = ((byte)(tmp50_46[tmp50_49] | 1 << portNum));
/* 136:    */     }
/* 137:    */     else
/* 138:    */     {
/* 139:210 */       int tmp67_66 = 0; byte[] tmp67_63 = gpioStatus.dat;tmp67_63[tmp67_66] = ((byte)(tmp67_63[tmp67_66] & (1 << portNum ^ 0xFFFFFFFF) & 0xF));
/* 140:    */     }
/* 141:212 */     int status = this.mFtDev.write(gpioStatus.dat, 1);
/* 142:213 */     return status;
/* 143:    */   }
/* 144:    */   
/* 145:    */   public int newWrite(int portNum, boolean bValue)
/* 146:    */   {
/* 147:219 */     gpio_dev gpioStatus = new gpio_dev();
/* 148:    */     
/* 149:221 */     int ftStatus = check(portNum);
/* 150:222 */     if (ftStatus != 0) {
/* 151:223 */       return ftStatus;
/* 152:    */     }
/* 153:225 */     if (!is_GPIOPort_Valid_Output(portNum)) {
/* 154:227 */       return 1015;
/* 155:    */     }
/* 156:230 */     getStatus(gpioStatus);
/* 157:232 */     if (bValue)
/* 158:    */     {
/* 159:233 */       int tmp50_49 = 0; byte[] tmp50_46 = gpioStatus.dat;tmp50_46[tmp50_49] = ((byte)(tmp50_46[tmp50_49] | 1 << portNum));
/* 160:    */     }
/* 161:    */     else
/* 162:    */     {
/* 163:235 */       int tmp67_66 = 0; byte[] tmp67_63 = gpioStatus.dat;tmp67_63[tmp67_66] = ((byte)(tmp67_63[tmp67_66] & (1 << portNum ^ 0xFFFFFFFF) & 0xF));
/* 164:    */     }
/* 165:237 */     if (this.iSignal)
/* 166:    */     {
/* 167:238 */       int tmp93_92 = 0; byte[] tmp93_89 = gpioStatus.dat;tmp93_89[tmp93_92] = ((byte)(tmp93_89[tmp93_92] | 0x8));
/* 168:    */     }
/* 169:    */     else
/* 170:    */     {
/* 171:240 */       int tmp109_108 = 0; byte[] tmp109_105 = gpioStatus.dat;tmp109_105[tmp109_108] = ((byte)(tmp109_105[tmp109_108] & 0x7));
/* 172:    */     }
/* 173:242 */     int status = this.mFtDev.write(gpioStatus.dat, 1);
/* 174:243 */     this.iSignal = (!this.iSignal);
/* 175:    */     
/* 176:245 */     return status;
/* 177:    */   }
/* 178:    */   
/* 179:    */   int check(int portNum)
/* 180:    */   {
/* 181:252 */     chiptop_mgr chipStatus = this.mFT4222Device.mChipStatus;
/* 182:254 */     if ((chipStatus.chip_mode == 2) || (chipStatus.chip_mode == 3)) {
/* 183:255 */       return 1013;
/* 184:    */     }
/* 185:257 */     if (portNum >= 4) {
/* 186:258 */       return 1014;
/* 187:    */     }
/* 188:260 */     return 0;
/* 189:    */   }
/* 190:    */   
/* 191:    */   int getStatus(gpio_dev gpioStatus)
/* 192:    */   {
/* 193:265 */     byte[] buf = new byte[8];
/* 194:    */     
/* 195:267 */     int ftStatus = cmdGet(32, 0, buf, 8);
/* 196:268 */     gpioStatus.usb.ep_in = buf[0];
/* 197:269 */     gpioStatus.usb.ep_out = buf[1];
/* 198:270 */     gpioStatus.mask = buf[5];
/* 199:271 */     gpioStatus.dir = buf[6];
/* 200:272 */     gpioStatus.dat[0] = buf[7];
/* 201:274 */     if (ftStatus == 8) {
/* 202:275 */       return 0;
/* 203:    */     }
/* 204:277 */     return ftStatus;
/* 205:    */   }
/* 206:    */   
/* 207:    */   void getGpioPinLevel(int portNum, byte data, boolean[] value)
/* 208:    */   {
/* 209:282 */     value[0] = IntToBool((data & 1 << portNum) >> portNum & 0x1);
/* 210:    */   }
/* 211:    */   
/* 212:    */   boolean is_GPIOPort(int portNum)
/* 213:    */   {
/* 214:287 */     chiptop_mgr chipStatus = this.mFT4222Device.mChipStatus;
/* 215:288 */     boolean ret = true;
/* 216:290 */     switch (chipStatus.chip_mode)
/* 217:    */     {
/* 218:    */     case 0: 
/* 219:293 */       if (((portNum == 0) || (portNum == 1)) && (
/* 220:294 */         (chipStatus.function == 1) || (chipStatus.function == 2))) {
/* 221:295 */         ret = false;
/* 222:    */       }
/* 223:296 */       if ((IntToBool(chipStatus.enable_suspend_out)) && (portNum == 2)) {
/* 224:297 */         ret = false;
/* 225:    */       }
/* 226:298 */       if ((IntToBool(chipStatus.enable_wakeup_int)) && (portNum == 3)) {
/* 227:299 */         ret = false;
/* 228:    */       }
/* 229:300 */       break;
/* 230:    */     case 1: 
/* 231:302 */       if ((portNum == 0) || (portNum == 1)) {
/* 232:303 */         ret = false;
/* 233:    */       }
/* 234:304 */       if ((IntToBool(chipStatus.enable_suspend_out)) && (portNum == 2)) {
/* 235:305 */         ret = false;
/* 236:    */       }
/* 237:306 */       if ((IntToBool(chipStatus.enable_wakeup_int)) && (portNum == 3)) {
/* 238:307 */         ret = false;
/* 239:    */       }
/* 240:308 */       break;
/* 241:    */     case 2: 
/* 242:    */     case 3: 
/* 243:311 */       ret = false;
/* 244:    */     }
/* 245:315 */     return ret;
/* 246:    */   }
/* 247:    */   
/* 248:    */   boolean is_GPIOPort_Valid_Output(int portNum)
/* 249:    */   {
/* 250:320 */     gpio_dev gpioStatus = new gpio_dev();
/* 251:321 */     boolean ret = is_GPIOPort(portNum);
/* 252:    */     
/* 253:323 */     getStatus(gpioStatus);
/* 254:325 */     if (ret) {
/* 255:327 */       if ((gpioStatus.dir >> portNum & 0x1) != 1) {
/* 256:329 */         ret = false;
/* 257:    */       }
/* 258:    */     }
/* 259:333 */     return ret;
/* 260:    */   }
/* 261:    */   
/* 262:    */   boolean is_GPIOPort_Valid_Input(int portNum)
/* 263:    */   {
/* 264:338 */     gpio_dev gpioStatus = new gpio_dev();
/* 265:339 */     boolean ret = is_GPIOPort(portNum);
/* 266:    */     
/* 267:341 */     getStatus(gpioStatus);
/* 268:343 */     if (ret) {
/* 269:345 */       if ((gpioStatus.dir >> portNum & 0x1) != 0) {
/* 270:347 */         ret = false;
/* 271:    */       }
/* 272:    */     }
/* 273:351 */     return ret;
/* 274:    */   }
/* 275:    */   
/* 276:    */   boolean update_GPIO_Status(int portNum, int gpioStatus)
/* 277:    */   {
/* 278:356 */     gpio_mgr gpio = new gpio_mgr();
/* 279:359 */     if (gpio.gpioStatus[portNum] != gpioStatus)
/* 280:    */     {
/* 281:361 */       char pullup = '\000';
/* 282:362 */       char pulldown = '\000';
/* 283:363 */       char opendrain = '\000';
/* 284:    */       
/* 285:365 */       gpio.gpioStatus[portNum] = gpioStatus;
/* 286:367 */       for (int idx = 0; idx < 4; idx++) {
/* 287:369 */         switch (gpio.gpioStatus[idx])
/* 288:    */         {
/* 289:    */         case 1: 
/* 290:372 */           pullup = (char)(pullup + (1 << idx));
/* 291:373 */           break;
/* 292:    */         case 2: 
/* 293:375 */           pulldown = (char)(pulldown + (1 << idx));
/* 294:376 */           break;
/* 295:    */         case 3: 
/* 296:378 */           opendrain = (char)(opendrain + (1 << idx));
/* 297:    */         }
/* 298:    */       }
/* 299:382 */       int ftStatus = cmdSet(34, pullup);
/* 300:383 */       ftStatus |= cmdSet(36, pulldown);
/* 301:384 */       ftStatus |= cmdSet(35, opendrain);
/* 302:386 */       if (ftStatus == 0) {
/* 303:388 */         gpio.gpioStatus[portNum] = gpioStatus;
/* 304:    */       }
/* 305:391 */       return ftStatus == 0;
/* 306:    */     }
/* 307:395 */     return true;
/* 308:    */   }
/* 309:    */   
/* 310:    */   boolean IntToBool(int i)
/* 311:    */   {
/* 312:399 */     return i != 0;
/* 313:    */   }
/* 314:    */ }



/* Location:           E:\code\demo\A108DevForBankComm\libs\d2xx.jar

 * Qualified Name:     com.ftdi.j2xx.ft4222.FT_4222_Gpio

 * JD-Core Version:    0.7.0.1

 */